# 中间件

from fastapi import FastAPI
from .usetime_middleware import UseTimeMiddleware
from .token_middleware import TokenMiddleware
from .test_middleware import TestMiddleware
from .jwt_middleware import JwtMiddleware


# 方案1
# 引用,并封装统一注册方法：
# def registerMiddlewareHandle(server:FastAPI):
#     # 添加耗时亲够中间件
#     server.add_middleware(UseTimeMiddleware)
#     # 添加耗时请求中间件
#     server.add_middleware(UseTimeMiddleware)
#     # 测试
#     server.add_middleware(TestMiddleware)
#     # jwt
#     server.add_middleware(JwtMiddleware)


# 方案2：
middlewareList = [
    UseTimeMiddleware,
    TokenMiddleware,
    TestMiddleware,
    JwtMiddleware
]


def registerMiddlewareHandle(server: FastAPI):
    # 倒序中间件
    middlewareList.reverse()
    # 遍历注册
    for _middleware in middlewareList:
        server.add_middleware(_middleware)


# 总结: 通过实践发现: 中间件的执行顺序和注册顺序，正好是相反的；先注册的后执行，

